package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.TmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class TmsController {
    @Autowired
    TmsService tmsService;
    @RequestMapping("/findPro")
    public Object findPro(String time) {
        List<Map> list = tmsService.findPro(time);
        System.out.println(list);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONArray dataArray = new JSONArray();

        JSONObject t1 = new JSONObject();
        t1.put("name","接单总数");
        t1.put("value",list.get(0).get("a"));
        dataArray.add(t1);
        jsonObject.put("data",dataArray);


        JSONObject t2 = new JSONObject();
        t2.put("name","发单总数");
        t2.put("value",list.get(0).get("b"));
        dataArray.add(t2);
        jsonObject.put("data",dataArray);

        JSONObject t3 = new JSONObject();
        t3.put("name","完成运单数");
        t3.put("value",list.get(0).get("c"));
        dataArray.add(t3);
        jsonObject.put("data",dataArray);

        return  jsonObject ;
    }

    @RequestMapping("/findCat")
    public Object findCat(String time) {
        List<Map> list = tmsService.findCat(time);
        System.out.println(list);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONArray dataArray = new JSONArray();

        for (Map map : list) {
            JSONObject t1 = new JSONObject();
            t1.put("name",map.get("n"));
            t1.put("value",map.get("zb"));
            dataArray.add(t1);
        }

        jsonObject.put("data",dataArray);
        return  jsonObject ;

    }

    @RequestMapping("/findCity")
    public Object findCity(String time) {
        List<Map> list = tmsService.findCity(time);
        System.out.println(list);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray xArray = new JSONArray();
        for (Map map : list) {
            xArray.add(map.get("n")) ;
        }
        dataObject.put("categories",xArray);

        JSONArray yArray = new JSONArray();

        JSONObject t1 = new JSONObject();
        t1.put("name","派送成功数");
        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            dataArray.add(map.get("num"));
        }
        t1.put("data",dataArray);
        yArray.add(t1);
        dataObject.put("series",yArray);
        jsonObject.put("data",dataObject);
        return  jsonObject ;
    }

    @RequestMapping("/findOrg")
    public Object findOrg(String time) {
        List<Map> list = tmsService.findOrg(time);
        System.out.println(list);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray TouArray = new JSONArray();

        JSONObject t1 = new JSONObject();
        t1.put("name","机构");
        t1.put("id","t1");
        TouArray.add(t1);

        JSONObject t2 = new JSONObject();
        t2.put("name","完运里程");
        t2.put("id","t2");
        TouArray.add(t2);

        JSONObject t3 = new JSONObject();
        t3.put("name","完运次数");
        t3.put("id","t3");
        TouArray.add(t3);

        JSONObject t4 = new JSONObject();
        t4.put("name","完运时长");
        t4.put("id","t4");
        TouArray.add(t4);

        JSONObject t5 = new JSONObject();
        t5.put("name","每次平均时长");
        t5.put("id","t5");
        TouArray.add(t5);


        JSONObject t6 = new JSONObject();
        t6.put("name","每次平均里程");
        t6.put("id","t6");
        TouArray.add(t6);
        dataObject.put("columns",TouArray);

        JSONArray rowArray = new JSONArray();
        for (Map map : list) {
            JSONObject t7 = new JSONObject();
            t7.put("t1",map.get("n"));
            t7.put("t2",map.get("total"));
            t7.put("t3",map.get("num"));
            t7.put("t4",map.get("totalTime"));
            t7.put("t5",map.get("avgTotal"));
            t7.put("t6",map.get("avgTime"));
            rowArray.add(t7);
        }





        dataObject.put("rows",rowArray);

        jsonObject.put("data",dataObject);
        return  jsonObject ;
    }
}
